export const diyMixin = {
  data() {
    return {
      swiperTypeVal: "",
      fixtop: null,
      cash1: null,
      bgColor: null
    };
  },
  computed: {
    menuButtonInfo() {
      // #ifndef APP-PLUS || H5
      let menuButtonInfo = uni.getMenuButtonBoundingClientRect();
      // #ifdef MP-TOUTIAO
      menuButtonInfo.top = `16rpx`;
      // #endif
      return menuButtonInfo;
      // #endif
    },
    moduleCss() {
      if (this.propsData && this.propsData.moduleCss) {
        return this.propsData.moduleCss;
      }
    },
    systemInfo() {
      try {
        const res = uni.getSystemInfoSync();
        return res;
      } catch (e) {
        // error
      }
    },
    outModuleCss() {
      if (!this.propsData) return {};
      let style = Object.assign({}, this.propsData.moduleCss);
      if (style.margin)
        style.margin = `${(style.marginTop || style.margin) * 2}rpx 0 ${
          style.margin * 2
        }rpx 0`;
      if (style.padding) style.padding = `0 ${style.padding * 2}rpx`;
      if (style.borderRadius)
        style["borderRadius"] = `${style.borderRadius * 2}rpx`;
      let obj = {
        margin: style.margin,
        borderRadius: style.borderRadius,
        padding: style.padding,
        position: "relative",
        overflow: "hidden"
      };
      return obj;
    },
    // 混入模块背景设置
    moduleBgCss() {
      uni.$on("swiperType", (data) => {
        this.swiperTypeVal = data.val;
      });
      uni.$on("fixedTop", (fixedTop) => {
        this.fixtop = fixedTop;
      });
      uni.$on("cash", (cash1) => {
        this.cash1 = cash1;
      });
      uni.$on("bgColor", (bgColor) => {
        this.bgColor = bgColor;
      });

      if (this.swiperTypeVal == "4" && this.fixtop == 1) {
        if (!this.propsData) return {};
        let style = Object.assign({}, this.propsData.moduleCss);
        if (!style.useBackground) {
          return {
            background: "inherit"
          };
        }
        let hSpace = style.padding;
        if (this.cash1 == 1) {
          let obj = {
            backgroundColor: this.bgColor,
            backgroundImage: style.backgroundImage || "",
            backgroundRepeat: style.backgroundRepeat || "no-repeat",
            backgroundSize: style.backgroundSize || "100% 100%",
            opacity: style.opacity || "",
            borderRadius: style.borderRadius * 2 + "rpx" || "",
            width: `calc(100% - ${hSpace * 4}rpx)`,
            left: hSpace * 2 + "rpx"
          };
          return obj;
        } else {
          let obj = {
            backgroundColor: "#ffffff00",
            backgroundImage: style.backgroundImage || "",
            backgroundRepeat: style.backgroundRepeat || "no-repeat",
            backgroundSize: style.backgroundSize || "100% 100%",
            opacity: style.opacity || "",
            borderRadius: style.borderRadius * 2 + "rpx" || "",
            width: `calc(100% - ${hSpace * 4}rpx)`,
            left: hSpace * 2 + "rpx"
          };
          return obj;
        }
      }

      if (this.swiperTypeVal == "4" && this.fixtop != 1) {
        if (!this.propsData) return {};
        let style = Object.assign({}, this.propsData.moduleCss);
        if (!style.useBackground) {
          return {
            background: "inherit"
          };
        }
        let hSpace = style.padding;
        let obj = {
          backgroundColor: "#ffffff00",
          backgroundImage: style.backgroundImage || "",
          backgroundRepeat: style.backgroundRepeat || "no-repeat",
          backgroundSize: style.backgroundSize || "100% 100%",
          opacity: style.opacity || "",
          borderRadius: style.borderRadius * 2 + "rpx" || "",
          width: `calc(100% - ${hSpace * 4}rpx)`,
          left: hSpace * 2 + "rpx"
        };
        return obj;
      }
      if (!this.propsData) return {};
      let style = Object.assign({}, this.propsData.moduleCss);
      if (!style.useBackground) {
        return {
          background: "inherit"
        };
      }
      let hSpace = style.padding;
      let obj = {
        backgroundColor: style.backgroundColor || "",
        backgroundImage: style.backgroundImage || "",
        backgroundRepeat: style.backgroundRepeat || "no-repeat",
        backgroundSize: style.backgroundSize || "100% 100%",
        opacity: style.opacity || "",
        borderRadius: style.borderRadius * 2 + "rpx" || "",
        width: `calc(100% - ${hSpace * 4}rpx)`,
        left: hSpace * 2 + "rpx"
      };
      return obj;
    },
    // 混入模块背景设置
    moduleBgNoCalcCss() {
      if (!this.propsData) return {};
      let style = Object.assign({}, this.propsData.moduleCss);
      if (!style.useBackground) {
        return {
          background: "inherit"
        };
      }
      let obj = {
        backgroundColor: style.backgroundColor || "",
        backgroundImage: style.backgroundImage || "",
        backgroundRepeat: style.backgroundRepeat || "no-repeat",
        backgroundSize: style.backgroundSize || "100% 100%",
        opacity: style.opacity || "",
        borderRadius: style.borderRadius * 2 + "rpx" || ""
      };
      return obj;
    }
  },
  filters: {
    priceBeforeZero(val) {
      if (val.indexOf(".") < 0) return `${val}.`;
      return val.substr(0, val.indexOf("."));
    },
    priceAfterZero(val) {
      if (val.indexOf(".") < 0) return "00";
      return val.substr(val.indexOf("."), val.length);
    }
  },
  methods: {
    fontCss(css) {
      if (css.fontNum && css.fontNum > 0) {
        css.fontSize = css.fontNum * 2 + "rpx";
        return css;
      } else {
        return css;
      }
    }
  }
};
